package com.geek.bulk.interceptor;

import com.geek.bulk.Retryer;
import com.geek.exception.RetryException;
import org.eclipse.microprofile.faulttolerance.Retry;

import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;


/**
 * 超时与重试（Timeout and Retry）
 */
@Interceptor
public class RetryInterceptor extends AnnotatedInterceptor<Retry> {

    @Override
    protected Object execute(InvocationContext context, Retry retry) throws Exception {
        Retryer retryer = Retryer.build(retry);
        while (true) {
            try {
                System.out.println("尝试次数 = " + retryer.getAttempt());
                return context.proceed();
            } catch (Exception e) {
                try {
                    retryer.continueOrPropagate(e);
                } catch (Exception e1) {
                    throw new RetryException(e);
                }
                continue;
            }
        }
    }

}
